package com.example.iosdatatimepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.iosdatatimepicker.WheelView.OnWheelScrollListener;
import com.example.iosdatatimepicker.WheelView.WheelView;
import com.example.iosdatatimepicker.WheelView.adapter.NumericWheelAdapter;

import java.util.Calendar;

/**
 * Created by 艳双 on 2016/2/18.
 * project:iosdatatimepicker
 * user:lisa[FR]
 * Date:2016/2/18
 */
public class CanlderActivity extends Activity implements View.OnClickListener {
    private WheelView year;
    private WheelView month;
    private WheelView day;
    private int mYear=1996;
    private int mMonth=0;
    private int mDay=1;
    private TextView riqi;
    private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canlder);
        riqi= (TextView) findViewById(R.id.riqi);
        btn= (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
        year = (WheelView)findViewById(R.id.year);
        month = (WheelView)findViewById(R.id.month);
        day = (WheelView)findViewById(R.id.day);
        getDataPick();

    }
    private  void getDataPick() {
        Calendar c = Calendar.getInstance();
        int norYear = c.get(Calendar.YEAR);

        int curYear = mYear;
        int curMonth =mMonth+1;
        int curDate = mDay;



        NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(this,1950, norYear);
        numericWheelAdapter1.setLabel("年");
        year.setViewAdapter(numericWheelAdapter1);
        year.setCyclic(true);
        year.addScrollingListener(scrollListener);


        NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(this,1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        month.setViewAdapter(numericWheelAdapter2);
        month.setCyclic(true);
        month.addScrollingListener(scrollListener);


        initDay(curYear,curMonth);
        day.setCyclic(true);
        day.addScrollingListener(scrollListener);


        year.setVisibleItems(7);
        month.setVisibleItems(7);
        day.setVisibleItems(7);

        year.setCurrentItem(curYear - 1950);
        month.setCurrentItem(curMonth - 1);
        day.setCurrentItem(curDate - 1);

    }
    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = year.getCurrentItem() + 1950;//年
            int n_month = month.getCurrentItem() + 1;//月
             initDay(n_year, n_month);
            String birthday=new StringBuilder().append((year.getCurrentItem()+1950)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem()+1) < 10) ? "0" + (day.getCurrentItem()+1) : (day.getCurrentItem()+1)).toString();
            riqi.setText(birthday);
        }
    };
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    /**
     */
    private void initDay(int arg1, int arg2) {
        NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
        numericWheelAdapter.setLabel("日");
        day.setViewAdapter(numericWheelAdapter);
    }

    @Override
    public void onClick(View view) {
       getDataPick();
    }
}